package com.bw.mallshop.mvvm.viewmodel;

import android.text.TextUtils;

import androidx.databinding.ObservableField;

import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.mallshop.dao.UserSingle;
import com.bw.mallshop.entity.RegisterEntity;
import com.bw.mallshop.entity.UserEntity;
import com.bw.mallshop.field.EventAction;
import com.bw.mallshop.mvvm.model.RegisterModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RegisterViewModel extends BaseViewModel<RegisterModel> {
    public ObservableField<String>phone = new ObservableField<>();
    public ObservableField<String>timeStr = new ObservableField<>();
    public ObservableField<String>pwd = new ObservableField<>();
    public ObservableField<String>confirmpwd = new ObservableField<>();
    public ObservableField<String>verify = new ObservableField<>();

    @Override
    protected void initData() {

    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof RegisterEntity){
            RegisterEntity registerEntity = (RegisterEntity) entity;
            if (registerEntity.message.equals("注册成功")){
                //正常逻辑用户详情信息->接口注册成功，只返回注册成功提示
                //在请求登录接口->用户信息
                Map<String,Object>map = new HashMap<>();
                map.put("mobile",phone.get());
                map.put("pushId","");
                map.put("pwd",pwd.get());
                m.requestLogin(map).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            }
        }else if (entity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) entity;
            //用户登录成功
            if (userEntity.status == 0){
                //接收完成用户数据
                UserSingle.getInstance().login(userEntity.data);
                //用户信息保存到本地
                m.insertUser(userEntity).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this);
            }else {
                showErro(userEntity.message);
            }
        }else {
            finishPage();
            EventAction action = new EventAction();
            action.action = "refresh_user";
            EventBus.getDefault().post(action);
            EventAction refresh = new EventAction();
            refresh.action = "refresh_car";
            EventBus.getDefault().post(refresh);
        }
    }

    public void verify(){

    }

    public void finish(){
        finishPage();
    }

    //注册用户方法
    public void confirm(){
        if (TextUtils.isEmpty(phone.get())){
            showErro("手机号为空");
            return;
        }
        if (TextUtils.isEmpty(pwd.get())){
            showErro("手机号为空");
            return;
        }
        Map<String,Object>map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("pwd",pwd.get());
        map.put("verifyCode","123456");
        m.requestRegister(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
}
